// pages/mys/guo/crop/add/index.js
const app = getApp(); // 获取应用实例
var Utils = require('../../../../../utils/crypt.js'); //加密解密

Page({

  /**
   * 页面的初始数据
   */
  data: {
    vcover: '', vlink: '', title: '',
    tid: 0, tname: '', lists: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.ctx = wx.createCameraContext();
    this.getIndex();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //返回
  toBack: function () {
    console.log('返回');
    wx.showToast({
      title: '即将返回。。', icon: 'none'
    })
    wx.redirectTo({
      url: '/pages/mys/guo/crop/index'
    })
  },
  //果树列表
  getIndex: function () {
    var that = this;
    var uwx = app.globalData.usertest;
    var userType = app.globalData.userType;
    var formData = {
      wx: uwx, userType: userType
    };
    //请求数据
    wx.request({
      url: app.globalData.domain+'/my/guo/index',
      method: "POST",
      header: {'content-type': 'application/x-www-form-urlencoded'},
      data: formData,
      success: function (res) {
        // wx.hideLoading();
        console.log(res);
        if (res.data.code!=0) {
          wx.showToast({
            title: res.data.msg, icon: 'none',
          }); return
        } 
        that.setData({
          lists: res.data.data, 
        });
      }
    });
  },
  // 摄像功能
  getLocalVideo: function () {
    var that = this;
    wx.chooseVideo({
      maxDuration: 30,
      success:function(res){
        // 这个就是最终拍摄视频的临时路径了
        // var tempFilePath = res.tempFilePath;
        wx.showToast({
          title: '完成视频，开始上传..', icon: 'none',
        }); 
        // wx.hideLoading();
        var fp = {filepath:res.tempFilePath};
        that.upload(fp);
      },
      fail:function(){
        console.error('获取本地视频时出错');
        wx.showToast({
          title: '获取本地视频时出错', icon: 'none',
        });
      }
    })
  },
  //上传视频
  upload: function (e) {
    var that = this;
    wx.showToast({
      title: '上传中..', icon: 'none',
    }); 
    var formData = {
      wx: app.globalData.usertest,
    };
    wx.uploadFile({
      url: app.globalData.domain + '/upload', 
      filePath: e.filepath,
      name: 'file',
      formData: formData,
      success(res) {
        // wx.hideLoading();
        res = JSON.parse(res.data); //返回值解析
        console.log(res);
        // wx.showToast({
        //   title: '上传结束..'+res.code, icon: 'none',
        // }); return
        if (res.code!=0) {
          wx.showToast({
            title: '上传失败..', icon: 'none',
          }); return
        } 
        wx.showToast({
          title: '上传成功..', icon: 'none',
        }); 
        that.setData({
          vcover: res.data.cover, vlink: res.data.link,
        });
      }
    });
  },
  //标题
  inputName: function (e) {
    console.log(e);
    var that = this;
    var title = e.detail.value;
    that.setData({title:title});
  },
  //选中
  toCheck: function (e) {
    console.log(e);
    var that = this;
    var tid = e.currentTarget.dataset.tid;
    var tname = e.currentTarget.dataset.tname;
    that.setData({
      tid: tid, tname: tname,
    });
  },
  //保存提交
  formSave: function () {
    var that = this;
    var uwx = app.globalData.usertest;
    var userType = app.globalData.userType;
    var title = that.data.title;
    var cover = that.data.vcover;
    var link = that.data.vlink;
    var tid = that.data.tid;
    var tname = that.data.tname;
    var formData = {
      wx: uwx, userType: userType, 
      cover: cover, link: link, 
      title: title, tid: tid,
    };
    // console.log(formData); return
    wx.showToast({title: '开始保存..', icon: 'none'}); 
    //请求数据
    wx.request({
      url: app.globalData.domain+'/my/guo/crop/add',
      method: "POST",
      header: {'content-type': 'application/x-www-form-urlencoded'},
      data: formData,
      success: function (res) {
        wx.hideLoading();
        if (res.data.code!=0) {
          wx.showToast({
            title: res.data.msg, icon: 'none',
          }); return
        } 
        wx.showToast({
          title: '保存成功..', icon: 'none',
        }); 
        setTimeout(function(){
          wx.redirectTo({
            url: '/pages/mys/guo/crop/index'
          });
        },2000);
      }
    });
  },
})